/************************************************************************************/
/*  Program name CAPGLIMP_SOB.sql                                        	        */
/*  Creater:  NIP Karen (PCCW - UniHub)                                         	*/
/*************   History   *************************                                */
/* 23 JAN, 2006   NIP Karen         First created                                   */
/* 10 FEB, 2006   Charissa			Added insert into security table				*/
/************************************************************************************/
SET SERVEROUTPUT ON SIZE 1000000;

ALTER TRIGGER GL_SETS_OF_BOOKS_CT1 DISABLE;

DECLARE
	lv_Prog_ID		VARCHAR2(100)	:=	'CAPGLIMP_SOB';

	lv_Scc_Count	NUMBER	:=	0;
	lv_Err_Count	NUMBER	:=	0;
	lv_Err_Msg		VARCHAR2(500)	:=	'No Error';

	CURSOR Cur_F IS
	SELECT
		*
	FROM
		CRST_GL_SETS_OF_BOOKS;

	-- 10-FEB-2006 start
	CURSOR Cur_G IS
	SELECT
		A.*,
		B.NAME_N
	FROM
		CGL_SETS_OF_BOOKS		A,
		CRST_GL_SETS_OF_BOOKS_MAP	B
	WHERE	A.NAME	=	B.NAME_N;
	-- 10-FEB-2006 end		
	
	lv_Next_Seq_ID		NUMBER;
	lv_COA_ID			NUMBER;

BEGIN
	UPDATE
		GL_SETS_OF_BOOKS
	SET	
		NAME		=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(SHORT_NAME,5,4), SHORT_NAME),
		SHORT_NAME	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(SHORT_NAME,5,4), SHORT_NAME)
	WHERE SET_OF_BOOKS_ID IN
		(
		SELECT 
			SET_OF_BOOKS_ID
		FROM 
			CRST_GL_SETS_OF_BOOKS
		);
		
	UPDATE
			CGL_SETS_OF_BOOKS
		SET	
			NAME		=	SUBSTR(NAME,1,LENGTH(NAME) - 4)||S_BUSINESS_GROUP_ID
			--SHORT_NAME	=	CAPBRIMP_MAPPING.RenameStr(SUBSTR(SHORT_NAME,5,4), SHORT_NAME)
		WHERE NAME IN
			(
			SELECT 
				NAME_N
			FROM 
				CRST_GL_SETS_OF_BOOKS_MAP
			);
	
	FOR C1 IN Cur_F LOOP
		BEGIN
			SELECT	GL_SETS_OF_BOOKS_S.NextVal
			INTO	lv_Next_Seq_ID
			FROM	DUAL;

			INSERT	INTO CRST_GL_SETS_OF_BOOKS_MAP	VALUES
			(
			C1.SET_OF_BOOKS_ID,
			CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.SHORT_NAME,5,4), C1.SHORT_NAME),
			--CAPBRIMP_MAPPING.RenameStr(SUBSTR(C1.NAME,1,4), C1.NAME),
			lv_Next_Seq_ID,
			C1.NAME 
			);
			
			lv_COA_ID	:=	CAPBRIMP_COMMON.GetCOAIDfromFlexStrCode(C1.ID_FLEX_STR_CODE);

			INSERT INTO GL_SETS_OF_BOOKS
			VALUES
			(
			lv_Next_Seq_ID,
			C1.NAME,
			C1.short_name,
			lv_COA_ID,
			C1.currency_code,
			C1.period_set_name,
			C1.accounted_period_type,
			C1.suspense_allowed_flag,
			C1.allow_intercompany_post_flag,
			C1.track_rounding_imbalance_flag,
			C1.enable_average_balances_flag,
			C1.enable_budgetary_control_flag,
			C1.require_budget_journals_flag,
			C1.enable_je_approval_flag,
			C1.enable_automatic_tax_flag,
			C1.consolidation_sob_flag,
			C1.translate_eod_flag,
			C1.translate_qatd_flag,
			C1.translate_yatd_flag,
			C1.mrc_sob_type_code,
			C1.allow_posting_warning_flag,
			C1.last_update_date,
			CAPBRIMP_COMMON.GetUser(C1.LAST_UPDATED_NAME),
			C1.creation_date,
			CAPBRIMP_COMMON.GetUser(C1.CREATED_NAME),
			C1.last_update_login,
			C1.future_enterable_periods_limit,
			C1.latest_opened_period_name,
			C1.latest_encumbrance_year,
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.ret_earn_ccid_d),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.cum_trans_ccid_d),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.res_encumb_ccid_d),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.net_income_ccid_d),
			CAPBRIMP_COMMON.GetSegment(lv_COA_ID,C1.rounding_ccid_d),
			C1.transaction_calendar_id,
			C1.daily_translation_rate_type,
			C1.earliest_untrans_period_name,
			C1.description,
			C1.attribute1,
			C1.attribute2,
			C1.attribute3,
			C1.attribute4,
			C1.attribute5,
			C1.attribute6,
			C1.attribute7,
			C1.attribute8,
			C1.attribute9,
			C1.attribute10,
			C1.attribute11,
			C1.attribute12,
			C1.attribute13,
			C1.attribute14,
			C1.attribute15,
			C1.context,
			C1.global_attribute_category,
			C1.global_attribute1,
			C1.global_attribute2,
			C1.global_attribute3,
			C1.global_attribute4,
			C1.global_attribute5,
			C1.global_attribute6,
			C1.global_attribute7,
			C1.global_attribute8,
			C1.global_attribute9,
			C1.global_attribute10,
			C1.track_entered_func_curr_flag
			);
		EXCEPTION
			WHEN OTHERS THEN
				DBMS_OUTPUT.Put_Line(SQLERRM);
				lv_Err_Count	:=	lv_Err_Count + 1;
		END;
		lv_Scc_Count	:=	lv_Scc_Count	+	1;
	END LOOP;

	-- 10-FEB-2006 start
	FOR C2 IN Cur_G LOOP
		INSERT INTO CGL_SETS_OF_BOOKS VALUES
		(
			C2.NAME_N,
			CAPBRIMP_MAPPING.GetOrgMapping(C2.S_ORG_ID),
			CAPBRIMP_MAPPING.GetBGMapping(C2.S_BUSINESS_GROUP_ID)
		);
	END LOOP;
	-- 10-FEB-2006 End
	
	IF	lv_Err_Count	>	0	THEN
		ROLLBACK;
	END IF;

	INSERT INTO CAPCNV_LOG(
		PROG_ID,
		PROCESS_DATE,
		RECORD_PROCESSED,
		RECORD_REJECTED,
		RECORD_EXTRACTED,
		ERR_MSG
	) VALUES (
		lv_Prog_ID,
		SYSDATE,
		lv_Scc_Count,
		lv_Err_Count,
		lv_Scc_Count-lv_Err_Count,
		lv_Err_Msg
	);
	
	COMMIT;
END;
/

ALTER TRIGGER GL_SETS_OF_BOOKS_CT1 ENABLE;